package com.sorhis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sorhis.common.constant.Constants;
import com.sorhis.dto.SysRoleDTO;
import com.sorhis.entity.SysRole;
import com.sorhis.entity.SysRoleMenu;
import com.sorhis.mapper.SysRoleMapper;
import com.sorhis.mapper.SysRoleMenuMapper;
import com.sorhis.service.ISysRoleMenuService;
import com.sorhis.service.ISysRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xihao.ding
 * @since 2025-02-10
 */
@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    private final ISysRoleMenuService sysRoleMenuService;

    private final SysRoleMenuMapper sysRoleMenuMapper;
    /**
     * 租户角色列表
     * @param sysRoleDTO
     * @return
     */
    @Override
    public IPage findRoleAndTenantPageList(SysRoleDTO sysRoleDTO) {
        Page<SysRole> page = new Page(sysRoleDTO.getPageNum(), sysRoleDTO.getPageSize());
        return this.getBaseMapper().findRoleAndTenantPageList(page , sysRoleDTO);
    }

    @Override
    public IPage findPageList(SysRoleDTO sysRoleDTO) {
        Page<SysRole> page = new Page(sysRoleDTO.getPageNum(), sysRoleDTO.getPageSize());
        return this.getBaseMapper().findPageList(page , sysRoleDTO);
    }

    @Override
    @Transactional
    public Boolean saveOrUpateDomain(SysRoleDTO sysRoleDTO) {
        SysRole sysRole = new SysRole();
        BeanUtil.copyProperties(sysRoleDTO , sysRole);
        sysRole.setSystemType(Constants.EMS_CONFIG);
        return this.saveOrUpdate(sysRole);
    }

    @Override
    public Boolean del(String id) {
        LambdaUpdateWrapper<SysRole> updateWrapper = new LambdaUpdateWrapper<>();
        Assert.notEmpty(id , "ID不能为空");
        updateWrapper.eq(SysRole::getId , id);
        updateWrapper.set(SysRole::getDeleted , 1);
        return this.update(updateWrapper);
    }

    @Override
    @Transactional
    public Boolean roleMenuSave(SysRoleDTO sysRoleDTO) {
        Assert.notEmpty(sysRoleDTO.getMenuIds() , "菜单ID集合不能为空");
        //删除sysRoleMenu表里角色iD等于sysRoleDTO.getId()的数据
        sysRoleMenuMapper.deleteAbsoluteById(sysRoleDTO.getId());
        //循环sysRoleDTO里的menuId,并且插入sysRoleMenu表里
        List<SysRoleMenu> list = new ArrayList<>();
        for (String menuId : sysRoleDTO.getMenuIds()) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(sysRoleDTO.getId());
            sysRoleMenu.setMenuId(menuId);
            list.add(sysRoleMenu);
        }
        return sysRoleMenuService.saveBatch(list);
    }
}
